<?php
namespace app\controller;

use app\service\Config;
use app\service\Controller;

class IndexController extends Controller
{

    /**
     * checkLogin
     * @return bool
     */
    protected function checkLogin()
    {
        $token = $this->request->cookie('token');
        if(empty($token) || $token != get_token($this->request->header('user-agent'))){
            return false;
        }else{
            $expire = Config::getInstance()->get('token')['expire']??7200;
            $this->response->cookie('token',$token,time()+$expire);
            return true;
        }
    }

    /**
     * index
     * @return mixed
     */
    public function index()
    {
        if(!$this->checkLogin()){
            return $this->redirect('login');
        }
        return $this->render('index',['soft'=>Config::getInstance()->get('soft')]);
    }

    /**
     * soft
     * @return mixed
     */
    public function soft()
    {
        if(!$this->checkLogin()){
            return $this->redirect('login');
        }

        $id = $this->request->get('id');
        $link = $this->request->get('action');

        $soft = Config::getInstance()->get('soft');

        if(empty($soft[$id]) || empty($linkData = $soft[$id]['link'][$link]??'')){
            return $this->error('参数错误.');
        }

        //修改配置文件
        if($this->request->isPost() && $link == 'config'){
            $path = $this->request->post('path');
            $content = $this->request->post('content');

            $configPathArray = array_column($this->getFileByDir($linkData['path']),'path');

            if(in_array($path,$configPathArray,true)){
                return file_put_contents($path,$content)?$this->success():$this->error('写入文件失败.');
            }else{
                return $this->error('参数错误.');
            }
        }

        if(!empty($linkData['command'])){
            $res = bash_exec($linkData['command']);
            if(!$res){
                return $this->error();
            }else{
                $res['type'] = 0;

                if($link == 'status'){
                    $res['status'] = $this->checkSoftStatus($res['output']);
                }else{
                    $res['status'] = $this->checkSoftStatus(bash_exec($soft[$id]['link']['status']['command']??null)['output']??'');
                }

                return $this->success($res);
            }
        }elseif (!empty($linkData['path'])){
            $res['type'] = 1;
            $res['file_data'] = $this->getFileByDir($linkData['path']);
            return $this->success($res);
        }else{
            return $this->error('未知错误.');
        }
    }

    /**
     * checkSoftStatus
     * @param $output
     * @return bool
     */
    protected function checkSoftStatus(string $output)
    {
        return !empty($output) && false !== stripos($output,'active (running)');
    }

    /**
     * getFileByDir
     * @param $pathArray
     * @return array
     */
    protected function getFileByDir($pathArray)
    {
        $fileData = [];
        foreach ($pathArray as $file){
            $fileData = array_merge($fileData,get_dir_files_content($file));
        }
       return $fileData;
    }

}